package Domain;
import java.io.*;
import java.util.*;

import javax.swing.JFileChooser;


public class Driver {
	LinkedHashMap<Character,LinkedHashMap<DfaState,DfaState>> table;
	LinkedHashMap<String,String> definedClassTokens,regexTokens;
	ScannerGenerator scannerGenerator;
	LinkedHashMap<String,DefinedClass> lex;
	NFADiagram nfaDiagram;
	DfaState start;
	public Driver()
	{
		table = null;
		definedClassTokens = null;
		regexTokens = null;
		nfaDiagram = null;
		scannerGenerator = new ScannerGenerator();
	}
	public void scanRegex(String fn)
	{
		String line;
		Scanner scan;
		File f = null;
		try{
			/*JFileChooser chooser = new JFileChooser();
			chooser.setDialogTitle("SELECT INPUT Specification FILE");
			int returnVal = chooser.showOpenDialog(null);
		    if(returnVal == JFileChooser.APPROVE_OPTION) {
		    	f= chooser.getSelectedFile();
		    }
			 */
			scan = new Scanner(new File(fn));
		
			//scan = new Scanner(fn);
		definedClassTokens = new LinkedHashMap<String,String>();
		regexTokens = new LinkedHashMap<String,String>();
		lex = new LinkedHashMap<String,DefinedClass>();
		if(scan.hasNextLine())
		{
			line = scan.nextLine();
			if(line.charAt(0)!='%'||line.charAt(1)!='%')
			{
				System.out.println("INPUT ERROR");
				
			}
		}
		while(scan.hasNextLine())
		{
			
			line = scan.nextLine();
			if(line.charAt(0)=='%'||line.charAt(1)=='%')
			{
				break;
			}
			line = line.trim();
			String[] tokens = line.split(" ", 2);
			tokens[1] = tokens[1].replaceAll("\\t", "");
			tokens[0] = tokens[0].replaceAll("\\t", "");
			definedClassTokens.put(tokens[0], tokens[1].trim());
			
		}
		while(scan.hasNextLine())
		{
			line = scan.nextLine();
			line = line.trim();
			String[] tokens = line.split(" ", 2);
			tokens[1] = tokens[1].replaceAll("\\t", "");
			tokens[0] = tokens[0].replaceAll("\\t", "");
			regexTokens.put(tokens[0], tokens[1].trim());
		}
		
		scan.close();
		}catch(Exception e){System.out.println("HERE"+e);}
		
	}
	
	public void scanInput(String fn)
	{
		Scanner scan;
		File f = null;
		try{
			/*JFileChooser chooser = new JFileChooser();
			chooser.setDialogTitle("SELECT INPUT FILE");
			int returnVal = chooser.showOpenDialog(null);
		    if(returnVal == JFileChooser.APPROVE_OPTION) {
		    	f= chooser.getSelectedFile();
		    }
			 */
		    scan = new Scanner(new File(fn));
			String token = "";
			while(scan.hasNext())
			{
				token = scan.next();
				DfaState temp = start;
				System.out.println(token);
				for(char c:token.toCharArray())
				{
					System.out.print(c);
					if(!table.containsKey(c))
					{
						System.out.println("Not a token");
						//break;
					}
					if(table.get(c).containsKey(temp))
						temp = table.get(c).get(temp);
					else
					{
						System.out.println("NOT A TOKEN: "+token);
						//break;
					}
					if(temp.isTerminal())
					{
						System.out.println("TOKEN "+token+" is TERMINAL!");
						//break;
					}
				}
			}
			scan.close();
		}catch(Exception e){System.out.println(e);}
	}
	
	public void run(String grammar,String input)
	{
		String test = "%% test \n $DIGIT   [0-9] \n $NON-ZERO    [^0]  IN  $DIGIT \n $LETTER [a-b] \n %% test \n $IDENTIFIER $LETTER+$DIGIT";
		
		scanRegex(grammar);
		
		nfaDiagram = scannerGenerator.createScanner(definedClassTokens, regexTokens);
		
		 LinkedList<Object> util = DFAConstructor.doSomething(nfaDiagram.getNfaStart());
		 table = (LinkedHashMap<Character,LinkedHashMap<DfaState,DfaState>>)util.get(1);
		 start = (DfaState)util.get(0);
		 String inputFile = "sample_code.txt";
		 scanInput(input);
		  
		
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Driver prgn= new Driver();
		if(args.length==0)
			prgn.run("/Developer/lisp/ScannerGenerator/src/Domain/sample_input_specification.txt", "/Developer/lisp/ScannerGenerator/src/Domain/sample_code.txt");
		else
			prgn.run(args[0],args[1]);
	}

}
